package com.copera.cn.kaka.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.copera.cn.kaka.ApplicationCustomer;

public class HttpUtils {
	private static CookieStore mCookie = null;
	public  static Bitmap getBitmapFromURL(String src) {
        try {
            Log.e("src",src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            Log.e("Bitmap","returned");
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Exception",e.getMessage());
            return null;
        }
    }
	public static String get(String url, HttpClient httpClient) throws ClientProtocolException, IOException{
		HttpGet httpGet = new HttpGet(url);
		httpClient.getParams().setParameter("http.socket.timeout",60000);
		if(mCookie != null){
			((DefaultHttpClient)httpClient).setCookieStore(mCookie);
			Log.i("Cookie", mCookie.toString());
		}
		HttpResponse httpResponse = httpClient.execute(httpGet);
		if(HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode()){
			
			mCookie = ((DefaultHttpClient)httpClient).getCookieStore();
			
			HttpEntity httpEntity = httpResponse.getEntity();
			return httpEntity == null ? null : EntityUtils.toString(httpEntity, ApplicationCustomer.CHARSET);
		}
		return null;
	}
	
	public static String post(String url, List<NameValuePair> pairs, HttpClient httpClient) throws ClientProtocolException, IOException{
		HttpPost httpPost = new HttpPost(url);
		if(pairs != null && pairs.size() > 0){
			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs, ApplicationCustomer.CHARSET);
			httpPost.setEntity(formEntity);
		}
		if(mCookie != null){
			((DefaultHttpClient)httpClient).setCookieStore(mCookie);
		}
		HttpResponse httpResponse = httpClient.execute(httpPost);
		if(HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode()){
			
			mCookie = ((DefaultHttpClient)httpClient).getCookieStore();
			
			HttpEntity httpEntity = httpResponse.getEntity();
			return httpEntity == null ? null : EntityUtils.toString(httpEntity, ApplicationCustomer.CHARSET);
		}
		return null;
	}
	
	public static InputStream getStreamFromUrl(String imgUrl, HttpClient httpClient) throws ClientProtocolException, IOException{
		HttpGet httpGet = new HttpGet(imgUrl);
		HttpResponse httpResponse = httpClient.execute(httpGet);
		if(HttpStatus.SC_OK == httpResponse.getStatusLine().getStatusCode()){
			HttpEntity httpEntity = httpResponse.getEntity();
			return httpEntity == null ? null : httpEntity.getContent();
		}
		return null;
	}
	
	public static void clearCookie(){
		mCookie = null;
	}
	public static String enCode(String source) throws Exception {
		String target = "";
		if (source != null && !source.equals("")) {
			target = new String(source.getBytes("ISO-8859-1"), "UTF-8");
		}
		return target;
	}
}
